// pages/personal/my-peccancy.js
const app = getApp()
const pool = new Map
Page({
    data: {
        tabs: [{
                label: "等待报价",
                status: 11
            },
            {
                label: "等待完成",
                status: 12
            },
            {
                label: "已完成",
                status: 13
            },
        ],
        statusObj: {
            1: "待报价",
            2: "待支付",
            3: "待处理",
            4: "待确认",
            5: "已完成",
            6: "已关闭",
        },
        current_status: 11,
        page: 1,
        total: 2,
        loading: false,
        list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    onShow() {
        pool.clear()
        this.loadData()
    },
    tab() {
        if (!pool.has(this.data.current_status)) {
            pool.set(this.data.current_status, {
                page: 1,
                total: 2,
                loading: false,
                list: []
            })
        }
        return pool.get(this.data.current_status)
    },
    clickTab(evt) {
        this.setData({
            current_status: evt.currentTarget.dataset.status
        })
        this.loadData()
    },
    loadData() {
        let tab = this.tab()
        this.setData(tab)
        let {
            orderString = 'createTime.desc', current_status = 1
        } = this.data
        if (tab.list.length >= tab.total || tab.loading) return
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('rest/ticket/querySelfList.do', {
            data: {
                page: tab.page,
                rows: 10,
                orderString,
                status: current_status
            }
        }).then(res => {
            tab.page++
                tab.total = res.obj.total
            tab.loading = false
            tab.list.push(...res.obj.rows)
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            this.setData({
                loading: false
            })
            wx.hideLoading()
        })
    },
    toDetail(evt) {
        let status = evt.currentTarget.dataset.status
        let id = evt.currentTarget.dataset.id
        let cid = evt.currentTarget.dataset.cid
        let url = ''
        // if (status == 1) {
        //     url = '/pages/car-owner/personal/offer-list?id=' + id
        // }
        if (status == 2) {
            url = '/pages/car-owner/peccancy/bond?id=' + id + '&cid=' + cid
        }
        // if (status == 3) {
        //     url = '/pages/car-owner/personal/pay-success?id=' + id
        // }
        if (status == 1 || status == 3 || status == 4 || status == 5 || status == 6) {
            url = '/pages/car-owner/personal/order-success?id=' + id
        }
        wx.navigateTo({
            url: url + '?id=' + id
        })
    },
    onPullDownRefresh: function() {
        const tab = this.tab()
        tab.page = 1
        tab.total = 2
        tab.list = []
        tab.loading = false
        this.loadData()
    },
    onReachBottom: function() {
        this.loadData()
    },
})